/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { XMLWriter } from "./htmlToXML.js";
import htmlParser from "./parser_html_json.js";


const html = `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .tagh1{
        background-color: aquamarine;
        color:'blue';
    }
    .one-div{
        line-height: 30px;
    }
</style>
<body>
    <h1 class="tagh1">
        kkkk
        <p>hhhhh</p>
    </h1>
    <div style="color:red; height:100px;" class="one-div">cshi</div>
    <img src="https:baidu.com" alt="wwww"/>
    <p>wjdwekfe</p>
    <em>dsjfw
    <div>dksfmjk</div>
    owqkdo</em>
</body>
</html>
`
const htmlliwang = `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .tagh1{
        background-color: aquamarine;
        color:'blue';
    }
    .one-div{
        line-height: 30px;
    }
</style>
<body><h1 class="tagh1">kkkk<p>hhhhh</p></h1><div style="color:red; height:100px;" class="one-div">cshi</div><img src="https:baidu.com" alt="wwww"/><p>wjdwekfe</p><em>dsjfw<div>dksfmjk</div>owqkdo</em></body></html>
`

const forNumber = 200;
const bigNumber = 10000;

function XMLWriter_test() {
    let property = [{
        key: XMLWriter.DOCTYPE_PUBLIC, value: '111'
    },
    {
        key: XMLWriter.DOCTYPE_SYSTEM, value: '11'
    }]
    let xml;
    let startTime = Date.now();
    for (let i = 0; i < bigNumber; i++) {
        xml = new XMLWriter(html, property);
    }
    xml.convertToXML((content, error) => {
        if (content == null) {
            throw new Error('Inconsistent with expected results')
        }
        // print(`${content}`)
        if (error) {
            let errorResult = 'error=' + JSON.stringify(error)
        }
    })
    let endTime = Date.now();
    print(`htmlToXML_XMLWriter: ${endTime - startTime} ms`)
}

function convertToXML_test() {
    let property = [{
        key: XMLWriter.DOCTYPE_PUBLIC, value: '111'
    },
    {
        key: XMLWriter.DOCTYPE_SYSTEM, value: '11'
    }]
    const xml = new XMLWriter(html, property);
    let startTime = Date.now();
    for (let i = 0; i < forNumber; i++) {
        xml.convertToXML((content, error) => {
            if (content == null) {
                throw new Error('Inconsistent with expected results')
            }
            // print(`${content}`)
            if (error) {
                let errorResult = 'error=' + JSON.stringify(error)
            }
        })
    }

    let endTime = Date.now();
    print(`htmlToXML_ConvertToXML: ${endTime - startTime} ms`)
}

function htmlParser_test() {
    let parserJson;
    let startTime = Date.now()
    for (let i = 0; i < bigNumber; i++) {
        parserJson = new htmlParser(htmlliwang);
    }
    let result = JSON.stringify(parserJson.getClassStyleJson())
    let endTime = Date.now()
    // print(`${result}`)
    if (result != `{"tagh1":"background-color:aquamarine;color:'blue';","one-div":"line-height:30px;"}`) {
        throw new Error('Inconsistent with expected results')
    }
    print(`htmlToXML_htmlParser: ${endTime - startTime} ms`)
}

function getClassStyleJson_test() {
    let result;
    let startTime = Date.now()
    for (let i = 0; i < bigNumber; i++) {
        let parserJson = new htmlParser(htmlliwang);
        result = JSON.stringify(parserJson.getClassStyleJson())
    }
    let endTime = Date.now()
    // print(`${result}`)
    if (result != `{"tagh1":"background-color:aquamarine;color:'blue';","one-div":"line-height:30px;"}`) {
        throw new Error('Inconsistent with expected results')
    }
    print(`htmlToXML_GetClassStyleJson: ${endTime - startTime} ms`)
}

function getHtmlJson_test() {
    let result;
    let startTime = Date.now()
    for (let i = 0; i < forNumber; i++) {
        let parserJson = new htmlParser(htmlliwang);
        result = JSON.stringify(parserJson.getHtmlJson())
    }
    let endTime = Date.now()
    if (result.length != 501) {
        throw new Error('Inconsistent with expected results')
    }
    print(`htmlToXML_getHtmlJson: ${endTime - startTime} ms`)
}

let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    XMLWriter_test()
    convertToXML_test()
    htmlParser_test()
    getClassStyleJson_test()
    getHtmlJson_test()
}

ArkTools.waitAllJitCompileFinish();

XMLWriter_test()
convertToXML_test()
htmlParser_test()
getClassStyleJson_test()
getHtmlJson_test()